﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Text.RegularExpressions;
using EKETEAM.FrameWork;
using EKETEAM.Data;

/// <summary>
/// Pub 的摘要说明
/// </summary>
public class Pub
{
	public Pub()
	{
		//
		// TODO: 在此处添加构造函数逻辑
        //
	}    
    public static void checkPhone()
    {
        return;
        string userAgent = HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"].ToString().Replace(" ", "").ToLower();
        if (userAgent.IndexOf(";android") == -1 && userAgent.IndexOf("android;") == -1 && userAgent.IndexOf("iphone;") == -1 && userAgent.IndexOf("ipad;") == -1)
        {
            string url = HttpContext.Current.Request.Url.PathAndQuery;
            url = Regex.Replace(url, "/phone/", "/", RegexOptions.IgnoreCase);
            url = StaticURL(url);
            HttpContext.Current.Response.Redirect(url, true);           
        }
    }
    public static void checkPC()
    {
        return;
        string userAgent = HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"].ToString().Replace(" ", "").ToLower();
        if (userAgent.IndexOf(";android") > -1 || userAgent.IndexOf("android;") > -1 || userAgent.IndexOf("iphone;") > -1 || userAgent.IndexOf("ipad;") > -1)
        {
            string abspath = eBase.getAbsolutePath();
            string url = HttpContext.Current.Request.Url.PathAndQuery;
            url = Regex.Replace(url, abspath, abspath + "phone/", RegexOptions.IgnoreCase);
            url = StaticURL(url);
            HttpContext.Current.Response.Redirect(url, true);
        }
    }
    public static void checkOS()
    {
        string url=HttpContext.Current.Request.Url.PathAndQuery;
        string abspath = eBase.getAbsolutePath();
        //if (abspath.Length > 1) url = Regex.Replace(url, abspath , "/", RegexOptions.IgnoreCase);       
        string userAgent = HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"].ToString().Replace(" ", "").ToLower();
        if (url.ToLower().IndexOf("/phone/") > -1) //移动版
        {
            if (userAgent.IndexOf(";android") == -1 || userAgent.IndexOf("android;") == -1 || userAgent.IndexOf("iphone;") == -1 || userAgent.IndexOf("ipad;") == -1)
            {
                url = Regex.Replace(url, "/phone/", "/", RegexOptions.IgnoreCase);
                url = StaticURL(url);
                HttpContext.Current.Response.Redirect(url, true);
            }
        }
        else //电脑版
        {
            if (url.ToLower().IndexOf("/system/") == -1 && url.ToLower().IndexOf("/manage/") == -1)
            {
                if (userAgent.IndexOf(";android") > -1 || userAgent.IndexOf("android;") > -1 || userAgent.IndexOf("iphone;") > -1 || userAgent.IndexOf("ipad;") > -1)
                {
                    url = Regex.Replace(url, abspath, abspath + "phone/", RegexOptions.IgnoreCase);
                    url = StaticURL(url);
                    HttpContext.Current.Response.Redirect(url, true);
                }
            }
        }
    }
    public static string StaticURL(string content)
    {
        if (!eConfig.staticURL()) return content;
        //?lang=zh_chs&id=2&pid=1&page=1
        content = Regex.Replace(content, "(\\w+).aspx\\?lang=(\\w+)&id=(\\d+)&pid=(\\d+)&page=(\\d+)", "$1-$3-$4_$5.html?lang=$2", RegexOptions.IgnoreCase);
        //?lang=zh_chs&id=2&pid=1
        content = Regex.Replace(content, "(\\w+).aspx\\?lang=(\\w+)&id=(\\d+)&pid=(\\d+)", "$1-$3-$4.html?lang=$2", RegexOptions.IgnoreCase);
        //?lang=zh_chs&id=2&page=1
        content = Regex.Replace(content, "(\\w+).aspx\\?lang=(\\w+)&id=(\\d+)&page=(\\d+)", "$1-$3_$4.html?lang=$2", RegexOptions.IgnoreCase);
        //?lang=zh_chs&id=2
        content = Regex.Replace(content, "(\\w+).aspx\\?lang=(\\w+)&id=(\\d+)", "$1-$3.html?lang=$2", RegexOptions.IgnoreCase);

        //?id=2&pid=1&page=1
        content = Regex.Replace(content, "(\\w+).aspx\\?id=(\\d+)&pid=(\\d+)&page=(\\d+)", "$1-$2-$3_$4.html", RegexOptions.IgnoreCase);
        //?id=2&pid=1
        content = Regex.Replace(content, "(\\w+).aspx\\?id=(\\d+)&pid=(\\d+)", "$1-$2-$3.html", RegexOptions.IgnoreCase);
        //?id=2&page=1
        content = Regex.Replace(content, "(\\w+).aspx\\?id=(\\d+)&page=(\\d+)", "$1-$2_$3.html", RegexOptions.IgnoreCase);
        //?id=2
        content = Regex.Replace(content, "(\\w+).aspx\\?id=(\\d+)", "$1-$2.html", RegexOptions.IgnoreCase);
        //default.aspx
        content = Regex.Replace(content, "(\\w+).aspx", "$1.html", RegexOptions.IgnoreCase);
        return content;
    }
    public static string getTablePrefix()
    {       
        return "eWeb_";
    }

    public static string getContent(string content)
    {
        string absPath = eBase.getAbsolutePath();
        content = content.Replace("x=images", "x=" + absPath + "images");
        content = content.Replace("f=upload", "f=" + absPath + "upload");
        content = Regex.Replace(content, "upload/",absPath + "upload/", RegexOptions.IgnoreCase);
        content = handlePlayer(content);
        return content;
    }
    public static string handlePlayer(string content)
    {
        string nr = content;
        #region 内容视频处理
        Regex reg = new Regex(@"\<embed(.*?)\>", RegexOptions.IgnoreCase);
        for (Match c = reg.Match(nr); c.Success; c = c.NextMatch())
        {
            string html = c.Groups[0].Value;


            string src = "", width = "", height = "", type = "";
            Match ma = Regex.Match(html, @"f=([\s\S]*?)&", RegexOptions.IgnoreCase);
            if (ma.Success) src = ma.Groups[1].Value;
            ma = Regex.Match(html, "width=\"([\\s\\S]*?)\"", RegexOptions.IgnoreCase);
            if (ma.Success) width = ma.Groups[1].Value;
            ma = Regex.Match(html, "height=\"([\\s\\S]*?)\"", RegexOptions.IgnoreCase);
            if (ma.Success) height = ma.Groups[1].Value;
            ma = Regex.Match(html, "type=\"([\\s\\S]*?)\"", RegexOptions.IgnoreCase);
            if (ma.Success) type = ma.Groups[1].Value;
            if (type.IndexOf("x-ms-asf-plugin") > -1)
            {
                ma = Regex.Match(html, "src=\"([\\s\\S]*?)\"", RegexOptions.IgnoreCase);
                if (ma.Success) src = ma.Groups[1].Value;
                html = "<audio id=\"audio\" src=\"" + src + "\" controls=\"controls\">" + html + "</audio>";
            }
            else
            {
                html = "<video id=\"video\" src=\"" + src + "\" poster=\"\" width=\"" + width + "\" height=\"" + height + "\" controls=\"controls\" autoplay=\"\">" + html + "</video> ";
            }
            string pattern = Regex.Escape(c.Groups[0].Value);
            nr = Regex.Replace(nr, pattern, html, RegexOptions.IgnoreCase);
        }
        #endregion
        return nr;
    }
    public static string getType()
    {
        string file = eBase.getAspxFileName().ToLower();
        if (file.IndexOf("article.aspx") > -1) return "2";
        if (file.IndexOf("product.aspx") > -1) return "3";
        if (file.IndexOf("picture.aspx") > -1) return "4";
        if (file.IndexOf("case.aspx") > -1) return "5";
        if (file.IndexOf("video.aspx") > -1) return "6";

        return "0";
    }
    /// <summary>
    //清除页面缓存
    /// </summary>
    public static void clearPageCache()
    {
        Page page = (Page)HttpContext.Current.Handler;
        page.Response.Buffer = true;
        page.Response.ExpiresAbsolute = DateTime.Now.AddDays(-2);
        page.Response.Cache.SetExpires(DateTime.Now.AddDays(-2));
        page.Response.Expires = 0;
        if (page.Request.ServerVariables["HTTP_USER_AGENT"] != null)
        {
            if (page.Request.ServerVariables["HTTP_USER_AGENT"].ToLower().IndexOf("Firefox") > -1)
            {
                page.Response.CacheControl = "max-age=0";
            }
            else
            {
                page.Response.CacheControl = "no-cache";
            }
        }
    }
    
}